package net.zjitc.controller;

import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Order;
import net.zjitc.exception.OrderException;
import net.zjitc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 毛钰深
 * @Create 2021/12/27 11:05
 */

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PreAuthorize("hasAuthority('155')")
    @GetMapping("/orders")
    public ResponseResult selectAll(@RequestParam(required = false) String query,
                                    @RequestParam int pagenum, @RequestParam int pagesize,
                                    @RequestParam(required = false) Integer user_id,
                                    @RequestParam(required = false) String pay_status,
                                    @RequestParam(required = false) String is_send,
                                    @RequestParam(required = false) String order_fapiao_title,
                                    @RequestParam(required = false) String order_fapiao_company,
                                    @RequestParam(required = false) String order_fapiao_content,
                                    @RequestParam(required = false) String consignee_addr) {
        Order order = new Order();
        order.setUser_id(user_id);
        order.setPay_status(pay_status);
        order.setIs_send(is_send);
        order.setOrder_fapiao_title(order_fapiao_title);
        order.setOrderFapiaoCompany(order_fapiao_company);
        order.setOrder_fapiao_content(order_fapiao_content);
        order.setConsignee_addr(consignee_addr);
        return orderService.selectAll(query, pagenum, pagesize, order);
    }

    @PreAuthorize("hasAuthority('154')")
    @PutMapping("/orders/{id}")
    public ResponseResult updateOrder(@PathVariable Integer id, @RequestBody(required = false) Order order) {
        ResponseResult res;
        try {
            order.setOrderId(id);
            res = orderService.updateOrder(order);
        } catch (OrderException e) {
            res = new ResponseResult();
            res.setError(e.getMessage());
        }
        return res;
    }

    @PreAuthorize("hasAuthority('155')")
    @GetMapping("/orders/{id}")
    public ResponseResult selectById(@PathVariable Integer id) {
        ResponseResult res = new ResponseResult();
        try {
            Order order = orderService.selectById(id);
            res.setSuccess("获取成功", order);
        } catch (OrderException e) {
            res.setNotFound(e.getMessage());
        }
        return res;
    }

    @PreAuthorize("hasAuthority('154')")
    @PutMapping("/orders/address/{id}")
    public ResponseResult updateOrderAddress(@PathVariable Integer id,@RequestBody Order order){
        ResponseResult res;
        try {
            if (order.getConsignee_addr()==null){
                throw new OrderException("地址参数为空");
            }
            res=orderService.updateOrderAddress(id,order.getConsignee_addr());
        } catch (OrderException e) {
            res = new ResponseResult();
            res.setError(e.getMessage());
        }
        return res;
    }

    //添加订单?  109
}
